package com.ccl.iot.device;

import com.ccl.iot.R;
import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.bean.IrToBatchItemBean;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.ir.DBUtils;

import java.util.ArrayList;

import cn.com.broadlink.sdk.data.controller.BLDNADevice;

/**
 * 创建者     王浩
 * 创建时间   2017/5/19 14:47
 * 描述	     博联的红外设备
 */

public class IOTBLir extends IOTVirtualDevice {

    private BLDNADevice mBLDNADevice;       //博联sdk构建的红外设备
    private long mDbId;                     //数据库中的设备id
    private ArrayList<IOTSubBLir> mSubBLirs;    //当前红外设备的子设备集合
    private boolean mOnline;

    private OnCodeChangedListener mCodeChangedListener = null;

    /**
     * 子设备的按键红外码数量变化的回调接口
     */
    public interface OnCodeChangedListener {
        public void onCodeChanged(IOTSubBLir subBLir, CodeviewBean bean, boolean isNew);

        public void onSubDevDelete(IOTSubBLir subBLir);
    }

    public IOTBLir(long aDevID, long dbId, BLDNADevice irDev) {
        super(aDevID);
        mDbId = dbId;
        mBLDNADevice = irDev;
        mSubBLirs = new ArrayList<IOTSubBLir>();
        mOnline = true;
    }

    public void clearSubDev() {
        //        mSubBLirs.clear();
        //        for (IOTSubBLir subDev : mSubBLirs) {
        //            subDev.clearBatchCodes();
        //        }
    }

    /**
     * 添加红外码
     *
     * @param bean
     */
    public synchronized void addIrCode(CodeviewBean bean) {
        if (mSubBLirs.size() > 0) {
            int size = mSubBLirs.size();
            for (int i = 0; i < size; i++) {
                IOTSubBLir iotSubBLir = mSubBLirs.get(i);
                if (iotSubBLir.getSubId() == bean.SubDevID) {
                    //当前红外码属于已创建的子设备
                    iotSubBLir.setIRCode(bean);
                    if (mCodeChangedListener != null) {
                        mCodeChangedListener.onCodeChanged(iotSubBLir, bean, false);
                    }
                    return;
                }
            }
        }
        String devType = null;
        //当前红外码属于新的子设备, 则创建子设备
        if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(bean.DevType) && DBUtils.CODE_TYPE_FIXED.equals(bean.CodeType)) {
            devType = BlirConst.IR_SUBDEV_TYPE_NON_STATE_AIRCONDITION;
        } else {
            devType = bean.DevType;
        }
        IOTSubBLir subBLir = new IOTSubBLir(getID(), bean.DevID, bean.SubDevID, devType, mBLDNADevice, iRegisterInterface, this);
        subBLir.setIRCode(bean);
        mSubBLirs.add(subBLir);
        if (mCodeChangedListener != null) {
            mCodeChangedListener.onCodeChanged(subBLir, bean, true);
        }
    }

    public void deleteSubDev(IOTSubBLir subBLir) {
        if (mSubBLirs != null && mSubBLirs.size() > 0) {
            mSubBLirs.remove(subBLir);
        }
        if (mCodeChangedListener != null) {
            mCodeChangedListener.onSubDevDelete(subBLir);
        }
    }

    public synchronized void clearBatchCodes() {
        if (mSubBLirs.size() > 0) {
            for (IOTSubBLir subDev : mSubBLirs) {
                subDev.clearBatchCodes();
            }
        }
    }

    public synchronized boolean isContainCode(IrToBatchItemBean bean) {
        boolean isContain = false;
        if (mSubBLirs.size() > 0) {
            for (IOTSubBLir subDev : mSubBLirs) {
                isContain = subDev.isContainCode(bean);
                if (isContain) {
                    break;
                }
            }
        }
        return isContain;
    }

    public ArrayList<IOTSubBLir> getSubBLirs() {
        return mSubBLirs;
    }

    public void setOnCodeChangedListener(OnCodeChangedListener l) {
        mCodeChangedListener = l;
    }

    @Override
    public int getImageId() {
        return R.drawable.icon_ir_dev;
    }

    public void setRealOnline(boolean online) {
        mOnline = online;
    }

    public boolean getRealOnline() {
        return mOnline;
    }

    public String getDid() {
        return mBLDNADevice.getDid();
    }

    public String getPid() {
        return mBLDNADevice.getPid();
    }

    public long getDbId() {
        return mDbId;
    }

    public BLDNADevice getBLDNADevice() {
        return mBLDNADevice;
    }

    public synchronized ArrayList<CodeviewBean> getAllCheckedCodes() {
        ArrayList<CodeviewBean> allCheckedCodes = null;
        if (mSubBLirs.size() > 0) {
            for (IOTSubBLir subDev : mSubBLirs) {
                CodeviewBean batchCodes = subDev.getBatchCodes();
                if (batchCodes != null) {
                    if (allCheckedCodes == null) {
                        allCheckedCodes = new ArrayList<>();
                    }
                    allCheckedCodes.add(batchCodes);
                }
            }
        }
        return allCheckedCodes;
    }

}
